<?php
namespace Macaca\Controller\Admin;

use Macaca;

class Article extends Base {

    public function moduleArticle() {
        //'a_id','ac_id','a_name','title','content','keywords','description','add_time','top_sort','click'
        return new Macaca\Module\Admin\Article($this->app);
    }

    public function actionGetList() {
        $app = $this->app;
        $view = new Macaca\View\Admin\Article($app);
        $module = $this->moduleArticle();
        $linker = $this->linker;

        $data['title'] = 'Macaca 文章管理';
        $data['url_add'] = $linker->toFix('/admin/article/add');
        $data['url_edit'] = $linker->to('/admin/article/edit');
        $data['url_delete'] = $linker->to('/admin/article/delete');
        $data['url_browse'] = $linker->to('/admin/article/browse');

        $page = $linker->router->getParameter('page', 0);
        $pageUrlPre = $linker->to('/admin/article/');
        $itemCount = $module->getCount();
        $data['page_box'] =
            $view->view->page()
            ->setUrlPre($pageUrlPre)->setCurrentLayout('first-pre-list-next-last-total')
            ->getByList($page, $itemCount, 10);

        $data['article'] = $module->getArticleList($page, 10);
        $this->response->setBody($view->render($data));
    }

    public function actionAddItem() {
        $app = $this->app;
        $view = new Macaca\View\Admin\ArticleEditor($app);
        $module = $this->moduleArticle();
        $moduleCategory = new Macaca\Module\Admin\ArticleCategory($this->app);

        $data['title'] = '添加文章';
        $data['url_save'] = $this->linker->toFix('/admin/article/save');

        $data['article'] = $module->getEmptyItem();
        $data['category_list'] = $moduleCategory->getCategoryList(0);
        $data['is_top'] = false;
        $this->response->setBody($view->render($data));
    }

    public function actionGetItem() {
        $app = $this->app;
        $view = new Macaca\View\Admin\ArticleEditor($app);
        $module = $this->moduleArticle();
        $moduleCategory = new Macaca\Module\Admin\ArticleCategory($app);

        $data['title'] = '编辑文章';
        $data['url_save'] = $this->linker->toFix('/admin/article/save');

        $aId = $app->router()->getParameter('a_id');
        $data['article'] = $module->getItemById($aId);
        if (empty($data['article'])) {
            echo '<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;你要编辑的文章不存在！';
        }
        else {
            $data['category_list'] = $moduleCategory->getCategoryList($data['article']['ac_id']);
            $data['is_top'] = (($this->app->TIME - $data['article']['top_sort']) < 5 * 24 * 60 * 60);
            $this->response->setBody($view->render($data));
        }
    }

    public function actionDeleteItem() {
        $module = $this->moduleArticle();
        $nid = $this->app->router()->getParameter('a_id');

        if ($module->deleteItemById($nid)) {
            echo '<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;成功删除所选文章。';
        }
        else {
            echo '<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;文章删除失败!';
        }
    }

    public function actionBrowseItem() {
        echo '<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;该功能未实现!';

    }

    public function actionSaveItem() {
        $module = $this->moduleArticle();

        if (isset($_POST['is_top']) and strtolower($_POST['is_top']) == 'on') {
            $_POST['top_sort'] = $this->app->TIME;
        }
        else {
            $_POST['top_sort'] = 0;
        }

        if ($module->saveItem($_POST)) {
            echo '<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;文章保存成功!';
        }
        else {
            echo '<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;文章保存失败!';
        }

    }
} 